from types import FrameType
from .sync import PropertyExecManager


TRANSITION_EVENTS = {
    "UiComponent.click",
    "UiComponent.inputText",
}


propExecManager = PropertyExecManager()


def trace_call(frame: FrameType, event, arg):
    # 只关心函数调用事件
    if event == 'return':
        func_name = frame.f_code.co_name

        # 追踪函数 do_hypium_rpc
        if func_name == 'do_hypium_rpc':

            # 获取参数信息
            # print("📦 函数参数:")
            local_vars = frame.f_locals

            if local_vars.get("api_name") in TRANSITION_EVENTS:
                propExecManager.propExecInfo.api_name = local_vars["api_name"]
                propExecManager.propExecInfo.args = local_vars["args"]

                propExecManager.propExecInfo.state = "OnGoing"
                propExecManager.switch_controller("monkey")
                propExecManager.kea2_runing.wait()

            # func_obj = frame.f_globals.get('do_hypium_rpc')

            # print(f"  api_config: {local_vars.get('api_config', 'N/A')}")
            # print(f"  api_name: {local_vars.get('api_name', 'N/A')}")
            # print(f"  args: {local_vars.get('args', 'N/A')}")
            # print(f"  kwargs: {local_vars.get('kwargs', 'N/A')}")


            print("=" * 60)

    return trace_call